<?php

namespace app\common\model;

class ArticleCategory extends BaseModel
{
    protected $table = 'article_category';
    public static function getListByNavId($nav_id)
    {
        return self::where("nav_id", $nav_id)->order('sort desc')->select();
    }
    public static function childList()
    {
        $data = self::order('sort')->select();
        return list2child($data->toArray());
    }
    public static function treeList()
    {
        $data = self::order('sort')->select();
        return list2tree($data->toArray());
    }
    // 获取与分类与所有下级的分类id 包括自身
    public static function getChildIds($id)
    {
        return \app\common\model\ArticleCategory::where("`id` = ? OR FIND_IN_SET(?, `parent_ids`)", [$id,$id])->column('id');
    }
    // 获取与分类与所有下级的分类id 包括自身
    public static function getFirstByNavGid($nav_gid)
    {
        $nav_id = \app\common\model\Nav::getIdByGid($nav_gid);
        return self::where('nav_id', $nav_id)->value('id');
    }
    public static function getIdsByNavGid($gid)
    {
        $nav_id = \app\common\model\Nav::getIdByGid($gid);
        if($nav_id == null) return [];
        $list = self::getListByNavId($nav_id);
        $ids = [];
        foreach ($list as $v) {
            $ids = array_merge($ids,self::getChildIds($v['id']));
        }
        return array_unique($ids);
    }
    public static function getIdsByNavId($nav_id)
    {
        $list = self::getListByNavId($nav_id);
        $ids = [];
        foreach ($list as $v) {
            $ids = array_merge($ids,self::getChildIds($v['id']));
        }
        return array_unique($ids);
    }
}